import os
import re

from docx import Document
import matplotlib.pyplot as plt

"""
@FileName：大五人格.py
@Description：

@Author：HeYiQing
@Time：2024/5/13 21:51
"""


# 获取个人基本信息的姓名(name)
def get_name_DW(tables):
    # 个人基本信息
    info_table = tables[0]
    name = info_table.cell(0, 3).text
    return name


# 绘制折线图
def plot(x, y, title, x_label, y_label):
    y_norm = ['神经质', '外向性', '开放性', '宜人性', '严谨性']
    # 正确显示负号
    plt.rcParams['axes.unicode_minus'] = False
    # 使用黑体字体显示中文
    plt.rcParams['font.sans-serif'] = ['SimHei']
    # 创建图表和轴
    plt.figure(figsize=(30, 12))
    # 为了在x轴上使用字符串，我们需要一个索引列表
    indices = range(len(x))
    # 绘制折线图
    plt.plot(x, y, marker='o')
    # 设置x轴的标签为姓名
    plt.xticks(indices, x, rotation=45)
    # 添加标题和轴标签
    plt.title(title, fontsize=24)
    plt.xlabel(x_label, fontsize=24)
    plt.ylabel(y_label, fontsize=24)
    # 添加图例
    plt.legend(labels=y_norm)
    # 显示图形
    plt.grid(True)
    # 保存文件
    path = "F:/比赛/数学建模/24/repo/大五人格/测试/折线图/" + title + ".png"
    directory = os.path.dirname(path)
    if not os.path.exists(directory):
        os.makedirs(directory)
    plt.savefig(path)
    # 显示图表
    plt.show()
    # 关闭
    plt.close()


# 获取文件中所有表格
def get_tables(path, file_name):
    abs_file_path = os.path.join(path, file_name)
    file = Document(abs_file_path)
    tables = file.tables
    return tables


def get_test_data(tables):
    data_table = tables[1]
    score_list = []
    for item in range(1, 6):
        try:
            score_list.append(int(data_table.cell(item, 1).text))
        except Exception:
            score_list.append(0)
    return score_list


# 获取姓名
name_list_A = []
name_list_B = []


def table_op(STR, file_dir_A, file_dir_B, file_list_A, file_list_B):
    # 获取大五人格表
    file_table_A = []
    file_table_B = []
    for file_name in file_list_A:  #
        if STR in file_name:
            table_list = get_tables(file_dir_A, file_name)  #
            file_table_A.append(table_list)
    for file_name in file_list_B:  #
        if STR in file_name:
            table_list = get_tables(file_dir_B, file_name)  #
            file_table_B.append(table_list)
    for tables in file_table_A:
        name_list_A.append(get_name_DW(tables))
    for tables in file_table_B:
        name_list_B.append(get_name_DW(tables))

    A_list = []
    B_list = []
    A_B_list = []

    for tables in file_table_A:
        A_list.append(get_test_data(tables))
    for tables in file_table_B:
        B_list.append(get_test_data(tables))
    A_B_list = A_list + B_list
    A_B_name = name_list_A + name_list_B

    # plot(name_list_A, A_list, "大五人格-A-选项-姓名", "姓名", "选项")
    # plot(name_list_B, B_list, "大五人格-B-选项-姓名", "姓名", "选项")
    plot(A_B_name, A_B_list, "大五人格-A-B-选项-姓名", "姓名", "选项")


def get_paragraphs(path, file_name):
    abs_file_path = os.path.join(path, file_name)
    file = Document(abs_file_path)
    result_list = []
    paragraphs = list(file.paragraphs)
    for i, para in enumerate(paragraphs[:-1]):  # 遍历到倒数第二个段落，因为需要访问下一个段落
        if '【' in para.text or '】' in para.text:
            try:
                result = paragraphs[i + 1].text[-5]
            except Exception:
                result = '中'
            if result == '低':
                result_list.append(0)
            elif result == '高':
                result_list.append(2)
            else:
                result_list.append(1)
    return result_list


def paragraphs_op(STR, file_dir_A, file_dir_B, file_list_A, file_list_B):
    # 获取大五人格表
    file_table_A = []
    file_table_B = []
    file_paragraphs_A = []
    file_paragraphs_B = []
    for file_name in file_list_A:  #
        if STR in file_name:
            tables = get_tables(file_dir_A, file_name)
            file_table_A.append(tables)
            paragraphs_list = get_paragraphs(file_dir_A, file_name)  #
            file_paragraphs_A.append(paragraphs_list)
    for file_name in file_list_B:  #
        if STR in file_name:
            tables = get_tables(file_dir_B, file_name)
            file_table_B.append(tables)
            paragraphs_list = get_paragraphs(file_dir_B, file_name)  #
            file_paragraphs_B.append(paragraphs_list)
    for tables in file_table_A:
        name_list_A.append(get_name_DW(tables))
    for tables in file_table_B:
        name_list_B.append(get_name_DW(tables))
    # print(len(file_paragraphs_A))
    # print(len(file_paragraphs_B))
    # print(len(name_list_A))
    # print(len(name_list_B))
    # plot(name_list_A, file_paragraphs_A, '大五人格-A-测试结果-姓名', '姓名', '测试结果')
    # plot(name_list_B, file_paragraphs_B, '大五人格-B-测试结果-姓名', '姓名', '测试结果')
    # plot(name_list_A + name_list_B, file_paragraphs_A + file_paragraphs_B, '大五人格-A-B-测试结果-姓名', '姓名', '测试结果')
    name_list = name_list_A + name_list_B
    para_list = file_paragraphs_A + file_paragraphs_B
    N_list = []
    E_list = []
    O_list = []
    A_list = []
    C_list = []
    for i, res in enumerate(para_list):
        for j, item in enumerate(res):
            if item == 0:
                if j == 0:
                    N_list.append(name_list[i])
                elif j == 1:
                    E_list.append(name_list[i])
                elif j == 2:
                    O_list.append(name_list[i])
                elif j == 3:
                    A_list.append(name_list[i])
                else:
                    C_list.append(name_list[i])
    print(N_list)
    print(E_list)
    print(O_list)
    print(A_list)
    print(C_list)


if __name__ == '__main__':
    STR = "大五人格量表"
    file_dir_A = r"E:\code_special\Gitee\Math_C\repo\A组--69名测试者"
    file_dir_B = r"E:\code_special\Gitee\Math_C\repo\B组--79名测试者"
    file_list_A = os.listdir(file_dir_A)
    file_list_B = os.listdir(file_dir_B)
    # table_op(STR, file_dir_A, file_dir_B, file_list_A, file_list_B)
    # get_paragraphs(file_dir_A, 'A001---大五人格量表.docx')
    paragraphs_op(STR, file_dir_A, file_dir_B, file_list_A, file_list_B)
